/**
Author: udayan
 */

package com.lightonyoga;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ShowAasanDetails extends Activity {
	Context context;
	String aasanID;
	Aasan aasan = null;
	String TAG = "LightOnYoga: ShowAasanDetails";

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aasan);
		context = this.getApplicationContext();
		Bundle bundle = this.getIntent().getExtras();
		aasanID = (String) bundle.getSerializable("aasan");  //get the name
		//search in the LightOnYogaActivity.aasans object and display the data
		Log.i(TAG,"Aasan reuested = "+aasanID);
		if(LightOnYogaActivity.aasans == null) {
			LightOnYogaActivity.loadAasans();
		}

		int size = LightOnYogaActivity.aasans.size();
		for (int i=0;i <size ;i++) {
			if(((Aasan)LightOnYogaActivity.aasans.get(i)).getName().compareTo(aasanID)==0){
				aasan = (Aasan)LightOnYogaActivity.aasans.get(i);
			}
		}

		//if the data is not available in the aasan.csv asset file
		if(aasan == null) {
			Log.e(TAG,"Requested Aasan not found in the dataset = "+aasanID);
			return;
		}	

		//now set the UI
		setUI();
	}

	void setUI(){
		TextView tv;

		tv = (TextView) findViewById(R.id.aasanTitle);
		tv.setText(aasan.getName());

		tv = (TextView) findViewById(R.id.aasanOtherNames);
		tv.setText(aasan.getSansName()+", "+aasan.getEnglishName());

		tv = (TextView) findViewById(R.id.aasanIntroduction);
		tv.setText(aasan.getIntroduction());

		tv = (TextView) findViewById(R.id.aasanTechnique);
		tv.setText(aasan.getTechnique());

		tv = (TextView) findViewById(R.id.aasanEffects);
		tv.setText(aasan.getEffects());

		//TODO: Add images


		loadImages();


	}
	void loadImages() {
		ImageView iv; 

		View linearLayout =  findViewById(R.id.aasanLinearLayout);
		//LinearLayout layout = (LinearLayout) findViewById(R.id.info);

		ArrayList<String> images = aasan.getImages();
		int len = images.size();
		for (int i=0; i < len ;i++) {
			int id = getResources().getIdentifier(images.get(i).substring(0, images.get(i).length()-4), "drawable", getPackageName());
			if(id == 0) {
				Log.e(TAG,"No Valid image found for resource = "+images.get(i) +  "file is =" + images.get(i).substring(0, images.get(i).length()-4));
				continue;
			}
			iv = new ImageView(this);
			iv.setImageResource(id);
			iv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
			((LinearLayout) linearLayout).addView(iv);
		}


	}

}




